package com.fjwt.gz.mgr.ctrl.merchant;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.components.oss.model.OssFileConfig;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.MchInfoDTO;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.entity.MchInfoEntity;
import com.fjwt.gz.db.vo.MchInfoVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.ActivityService;
import com.fjwt.gz.service.impl.MchInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商户管理类
 *
 * @author pangxiaoyu
 * @site https://pt.jdyd.xyz/
 * @date 2021-06-07 07:15
 */
@Slf4j
@RestController
@RequestMapping("/api/mchInfo")
public class MchInfoController extends CommonCtrl {

    @Autowired
    private MchInfoService mchInfoService;
    @Autowired
    private ActivityService activityService;

    /**
     * 商户信息列表
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO')")
    @GetMapping
    public ApiRes list() {
        MchInfoDTO mchInfoDTO = getObject(MchInfoDTO.class);
        log.info("pc端商户列表查询条件，请求地址：/api/mchInfo，请求参数：dto={}", JSONUtil.toJsonStr(mchInfoDTO));
        IPage<MchInfoVO> pages = mchInfoService.getMchList(getIPage(), mchInfoDTO);
        log.info("pc端商户列表查询条件，请求地址：/api/mchInfo，返回结果：{}", JSONUtil.toJsonStr(pages));
        return ApiRes.page(pages);
    }



    /**
     * 商家信息修改
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_EDIT')")
    @PostMapping("/updateProcessMch")
    public ApiRes updateProcessMch() {
        PointDTO location = getVal("location", PointDTO.class);
        MchInfoEntity mchInfo = getObject(MchInfoEntity.class);
        if (mchInfo == null) {
            return ApiRes.customFail("网络波动，请稍后重试！");
        }
        return ApiRes.ok(activityService.updateProcessMch(mchInfo,location));
    }


    /**
     * 查询商户信息
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_VIEW')")
    @PostMapping("/detail")
    public ApiRes detail() {
         String mchNo = getValString("mchNo");
         log.info("pc端商户详情查询条件，请求地址：/api/mchInfo/detail，请求参数：{}", mchNo);
         MchInfoVO vo = mchInfoService.getMchInfo(mchNo);
         log.info("pc端商户详情查询条件，请求地址：/api/mchInfo/detial，返回结果：{}",JSONUtil.toJsonStr(vo));
         MchInfoVO data = new MchInfoVO();
         BeanUtils.copyProperties(vo, data);
         return ApiRes.ok(data);
    }


    /**
     * 导入平台商户数据
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_IMPT_MCH')")
    @PostMapping("/import")
    public ApiRes importMchInfo() {
        String ossFileUrl = getValString("ossFileUrl");
        log.info("pc端导入平台商户数据，请求地址：/api/mchInfo/import，请求参数：ossFileUrl={}", ossFileUrl);
        String currentAgentNo = getCurrentUser().getSysUser().getBelongInfoId();
        String failMchInfoUrl=mchInfoService.importMchInfo(ossFileUrl, OssFileConfig.BIZ_TYPE.UPLOAD,  currentAgentNo);
        log.info("pc端导入平台商户数据，请求地址：/api/mchInfo/import，返回结果：{}", failMchInfoUrl);
        return ApiRes.ok(failMchInfoUrl);
    }

    /**
     * 对比商户数据并导出
     * 时永浩
     * 2024-10-15
     */
    @PreAuthorize("hasAnyAuthority('ENT_OPER_TOOL_INFO_COMPARE_MCH')")
    @PostMapping("/compare")
    public ApiRes compareMchInfo() {
        String ossFileUrl = getValString("ossFileUrl");
        log.info("pc端对比商户数据并导出，请求地址：/api/mchInfo/compare，请求参数：ossFileUrl={}", ossFileUrl);
        JSONObject result = mchInfoService.compareMchInfo(ossFileUrl, response);
        log.info("pc端对比商户数据并导出，请求地址：/api/mchInfo/compare，返回结果：{}", result);
        return ApiRes.ok(result);
    }


    /**
     * 商户图片修改
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_IMG')")
    @PostMapping("/updateImage")
    public ApiRes updateImage() {
        String mchNo = getValString("mchNo");
        String imageUrl = getValString("imageUrl");
        log.info("pc端商户图片修改，请求地址：/api/mchInfo/updateImage，请求参数：mchNo={},imageUrl={}", mchNo, imageUrl);
        mchInfoService.updateMchImage(mchNo,imageUrl);
        return ApiRes.ok();
    }

    /**
     * 商详情户图片修改
     * @return
     */
    @PostMapping("/updateBannerImage")
    public ApiRes updateBannerImage() {
        String mchNo = getValString("mchNo");
        String mchBannerImage = getValString("mchBannerImage");
        log.info("pc端商户图片修改，请求地址：/api/mchInfo/updateImage，请求参数：mchNo={},bannerImageUrl={}", mchNo, mchBannerImage);
        mchInfoService.updateBannerImage(mchNo,mchBannerImage);
        return ApiRes.ok();
    }

    /**
     * 精选商户置顶
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_ORDER')")
    @PostMapping("/topMch")
    public ApiRes topMch() {
        String mchNo = getValString("mchNo");
        Integer order = getValInteger("order");
        log.info("pc端精选商户置顶，请求地址：/api/mchInfo/topMch，请求参数：mchNo={},order={}", mchNo, order);
        mchInfoService.updateMchOrderSort(mchNo,order);
        return ApiRes.ok();
    }

    /**
     * 商户进件信息
     *
     * @return
     */

}
